package org.flyking.common;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.appender.RollingFileAppender;
import org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy;
import org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy;
import org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy;
import org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.config.builder.api.*;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
import org.apache.logging.log4j.core.layout.PatternLayout;

public class Configure {
    static public boolean Log4j2Config(){
        // 获取Logger上下文
        LoggerContext context = (LoggerContext) LogManager.getContext(false);
        Configuration config = context.getConfiguration();

        // 1. 创建PatternLayout（日志格式）
        PatternLayout layout = PatternLayout.newBuilder()
                .withConfiguration(config)
                .withPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n")
                .build();

        // 2. 定义滚动策略
        // 基于时间的滚动策略（每天滚动）
        TimeBasedTriggeringPolicy timePolicy = TimeBasedTriggeringPolicy.newBuilder()
                .withInterval(1)  // 单位由filePattern中的日期格式决定（这里%d{yyyy-MM-dd}表示天）
                .withModulate(true)  // 调整到时间边界（如午夜）
                .build();

        // 基于大小的滚动策略（10MB）
        SizeBasedTriggeringPolicy sizePolicy = SizeBasedTriggeringPolicy.createPolicy("10 MB");

        // 组合策略（时间+大小）
        CompositeTriggeringPolicy policy = CompositeTriggeringPolicy.createPolicy(timePolicy, sizePolicy);

        // 3. 定义Rollover策略
        DefaultRolloverStrategy strategy = DefaultRolloverStrategy.newBuilder()
                .withMax("30")  // 保留30个归档文件
                .withConfig(config)
                .build();

        // 4. 创建RollingFileAppender
        RollingFileAppender appender = RollingFileAppender.newBuilder()
                .setName("RollingFile")  // Appender名称
                .withFileName("logs/app.log")  // 当前日志文件
                .withFilePattern("logs/archive/app-%d{yyyy-MM-dd}-%i.log.gz")  // 归档模式（带压缩）
                .withLayout(layout)
                .withPolicy(policy)  // 滚动策略
                .withStrategy(strategy)  // 归档策略
                .setConfiguration(config)
                .build();

        appender.start();  // 启动Appender

        ConsoleAppender consoleAppender=ConsoleAppender.newBuilder()
                .setName("Console")
                .setLayout(layout)
                .setTarget(ConsoleAppender.Target.SYSTEM_OUT)
                .setConfiguration(config)
                .build();
        consoleAppender.start();

        // 5. 将Appender添加到配置
        config.addAppender(appender);
        config.addAppender(consoleAppender);

        // 6. 更新Logger配置（应用到Root Logger）
        LoggerConfig rootLogger = config.getRootLogger();
        rootLogger.addAppender(appender, Level.DEBUG, null);  // 设置日志级别为INFO
        rootLogger.addAppender(consoleAppender,Level.DEBUG, null);
        context.updateLoggers();  // 应用新配置
        return true;
    }
}
